package org.mybatis.plus.spring;
 
import java.sql.Connection;

import org.aspectj.lang.ProceedingJoinPoint;
import org.mybatis.plus.core.SqlSessionFactory;
 
/**
* @desc: Transaction切面
**/
public class DataSourceTransactionManager {

	private SqlSessionFactory sqlSessionFactory;
	
    public SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	/**
     * @description  在连接点执行之后执行的通知（返回通知和异常通知的异常）
     */
	// 定义需要匹配的切点表达式，同时需要匹配参数
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("方法环绕start...around");
        Object result = null;
        Connection connection = sqlSessionFactory.getConnection();
        try{
            result = pjp.proceed();
            System.out.println("commit=="+connection);
            connection.commit();
        }catch (Throwable e){
            e.printStackTrace();
            System.out.println("rollback=="+connection);
            connection.rollback();
        }finally {
        	 System.out.println("close=="+connection);
        	connection.close();
        }
        System.out.println("方法环绕end...around");
        return result;
    }
 
}